<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>菜单管理</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css" />
    <link rel="stylesheet" href="/static/ui/css/ui.css" />
</head>

<body class="pear-container">

    <div class="layui-btn-container" style="display: inline-block;">
        <button class="layui-btn layui-btn-sm layui-btn-danger" id="add_menu">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-success" id="add_many">
            <i class="layui-icon layui-icon-add-1"></i>
            批量新增
        </button>
        <button id="btnExpandAll" class="layui-btn layui-btn-sm layui-btn-normal">
            <i class="layui-icon">&#xe668;</i>展开全部
        </button>
        <button id="btnFoldAll" class="layui-btn layui-btn-sm layui-btn-warm">
            <i class="layui-icon">&#xe66b;</i>折叠全部
        </button>
    </div>

    <div class="layui-card">
        <div class="layui-card-body">
            <table id="power-table" lay-filter="power-table"></table>
        </div>
    </div>


    <script type="text/html" id="power-bar">
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
                <i class="layui-icon layui-icon-edit"></i>
            </button>
            <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
                <i class="layui-icon layui-icon-delete"></i>
            </button>
            <button class="pear-btn pear-btn-success pear-btn-sm" lay-event="sql">
                <i class="layui-icon layui-icon-rss"></i>
            </button>
        </script>

    <script type="text/html" id="power-type">
            {{#if (d.type == '0') { }} <span> 目录</span> {{# }else if(d.type == '1'){ }} <span> 菜单</span> {{# }else if(d.type == '2'){ }} <span> 按钮</span> {{# }else if(d.type == '3'){ }} <span> 权限</span> {{# } }}
    </script>
    <script type="text/html" id="power-sys">
        {{#if (d.is_sys == '0') { }} <span> 系统</span> 
        {{# }else if(d.is_sys == '1'){ }} <span> 租户</span> 
        {{# }else if(d.is_sys == '2'){ }} <span> 会员</span> 
       {{# } }}
</script>
    <script type="text/html" id="power-enable">
            <input type="checkbox" name="ifshow" value="{{d.id}}" lay-skin="switch" lay-text="显示|不显示" lay-filter="ifshow"  {{ d.ifshow == 0 ? 'checked' : '' }}/>
        </script>

    <script type="text/html" id="icon">
            <i class="layui-icon {{d.icon}}"></i>
        </script>
    <script src="../config.js"></script>
    <script src="/static/layui/layui.js"></script>
    <script src="/static/ui/ui.js"></script>
    <script>
        layui.use([
            'table', 'form', 'jquery', 'treeTable'
        ], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let treeTable = layui.treeTable;

            let MODULE_PATH = "./";
            let insTb = treeTable.render({
                elem: '#power-table',
                toolbar: '#power-toolbar',
                tree: {
                    iconIndex: 1,  // 折叠图标显示在第几列
                    idName: 'id',  // 自定义id字段的名称
                    pidName: 'pid',  // 自定义标识是否还有子节点的字段名称
                    haveChildName: 'haveChild',  // 自定义标识是否还有子节点的字段名称
                    isPidData: true  // 是否是pid形式数据
                },
                cols: [
                    {
                        field: 'id',
                        title: 'ID',
                        width: 60
                    }, {
                        field: 'title',
                        title: '名称'
                    }, {
                        field: 'order_num',
                        edit: 'text',
                        width: 80,
                        title: '排序'
                    }, {
                        field: 'icon',
                        title: '图标',
                        width: 70,
                        templet: '#icon'
                    }, {
                        field: 'type',
                        title: '权限',
                        width: 100,
                        templet: '#power-type'
                    }, {
                        field: 'is_sys',
                        title: '类型',
                        width: 100,
                        templet: '#power-sys'
                    }, {
                        field: 'ifshow',
                        title: '是否显示',
                        width: 120,
                        templet: '#power-enable'
                    }, {
                        title: '操作',
                        templet: '#power-bar',
                        width: 150,
                        align: 'center'
                    }
                ],
                reqData: function (data, callback) {
                    // 在这里写ajax请求，通过callback方法回调数据
                    //console.log(data)
                    let pid = data ? data.id : 0;
                    _get(layui, 'menu/oplist?pid=' + pid, function (res) {
                        console.log(res)
                        callback(res.data);  // 参数是数组类型
                    });
                }
            });

            treeTable.on('tool(power-table)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                }
                else if (obj.event === 'sql') {
                    window.sql(obj);
                }
            })

            // 全部展开
            $('#btnExpandAll').click(function () {
                insTb.expandAll();
            });

            // 全部折叠
            $('#btnFoldAll').click(function () {
                insTb.foldAll();
            });
            $('#add_menu').click(function () {
                layer.open({
                    type: 2,
                    title: '新增',
                    shade: 0.1,
                    offset: 'rt',
                    area: [
                        '80%', '100%'
                    ],
                    anim: 1,
                    maxmin: true,
                    content: MODULE_PATH + 'add.html'
                });
            });
            $('#add_many').click(function () {
                layer.open({
                    type: 2,
                    title: '批量新增',
                    shade: 0.1,
                    offset: 'rt',
                    area: [
                        '80%', '100%'
                    ],
                    anim: 1,
                    maxmin: true,
                    content: MODULE_PATH + 'add-many.html'
                });
            });
            treeTable.on('edit(power-table)', function (obj) {
                //console.log(obj)

                let update = {
                    id: obj.data.id,
                    field: obj.field,
                    value: obj.value
                }
                //console.log(update)
                //return;
                _post(layui, 'menu/editData', update, res => {
                    //console.log(res)
                    //setRoute(res.routeData)
                    layer.msg('更新成功', {
                        icon: 1,
                        time: 1000
                    });
                }, err => {
                    //console.log(err)

                })
            });
            form.on('switch(ifshow)', function (data) {
                let v = 1;
                if (data.elem.checked)
                    v = 0;
                let postData = {
                    id: data.value,
                    ifshow: v
                };
                _post(layui, 'menu/ifshow', postData, res => {
                    //console.log(res)
                    //setRoute(res.routeData)
                }, err => {
                    $(data.elem).prop('checked', false);
                    form.render('checkbox');
                })
            });

            window.sql = function (obj) {
                layer.open({
                    type: 2,
                    title: '导出',
                    shade: 0.1,
                    offset: 'rt',
                    area: [
                        '80%', '100%'
                    ],
                    anim: 1,
                    maxmin: true,
                    content: MODULE_PATH + 'json.html?id=' + obj.data.id
                });
            }

            window.edit = function (obj) {
                //console.log(obj)
                layer.open({
                    type: 2,
                    title: '修改',
                    shade: 0.1,
                    offset: 'rt',
                    area: [
                        '80%', '100%'
                    ],
                    anim: 1,
                    maxmin: true,
                    content: MODULE_PATH + 'edit.html?id=' + obj.data.id
                });
            }
            window.remove = function (obj) {
                layer.confirm('确定要删除该权限', {
                    icon: 3,
                    title: '提示'
                }, (index) => {
                    layer.close(index);
                    _post(layui, 'menu/del', {
                        id: obj.data.id
                    }, res => {
                        //setRoute(res.routeData)
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        }, () => {
                            obj.del();
                        });
                    }, err => {
                        //layer.msg('删除失败',{icon:2,time:1000});
                    })
                });
            }


        })
    </script>
</body>

</html>